package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.userInfo.activity.AddProductionActivity;
import com.rongshu.view.userInfo.entity.PostVipAuthentication;
import com.rongshu.view.userInfo.listener.OnItemClickListener;
import com.rongshu.widget.RoundImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MaterialsAuthenticationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //context
    private Context context = null;
    //list
    private List<PostVipAuthentication.VipDateModeBean> vipDateModeBeanList = null;
    //listener
    private OnItemClickListener listener = null;

    public MaterialsAuthenticationAdapter(Context context) {
        this.context = context;
    }

    public void setVipDateModeBeanList(List<PostVipAuthentication.VipDateModeBean> list) {
        this.vipDateModeBeanList = list;
    }

    public void addVipDateModeBeanList(List<PostVipAuthentication.VipDateModeBean> list) {
        this.vipDateModeBeanList.addAll(list);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_materials_authentication_item_view, viewGroup, false);
        return new MaterialsAuthenticationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        MaterialsAuthenticationViewHolder holder = (MaterialsAuthenticationViewHolder) viewHolder;
        final PostVipAuthentication.VipDateModeBean bean = vipDateModeBeanList.get(i);
        holder.txt_name.setText("《" + bean.getDataName() + "》");
        holder.txt_platform.setText(bean.getDataRemarks());
        final int type = bean.getType();
        holder.roundImageView.setVisibility(type == 5 ? View.VISIBLE : View.GONE);
        if (type == 5) {
            String dataImg = bean.getDataImg();
            String imgUrl = null;
            if (dataImg.contains("|")) {
                imgUrl = dataImg.substring(0, dataImg.indexOf("|"));
            } else {
                imgUrl = dataImg;
            }
            GlideUtils.loadImageView(context, holder.roundImageView, imgUrl,
                    R.drawable.penpal_default_logo, R.drawable.penpal_default_logo);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(type, i, bean);
                }
            }
        });
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(viewHolder, position, payloads);
        if (payloads.isEmpty()) {
            onBindViewHolder(viewHolder, position);
        } else {
            MaterialsAuthenticationViewHolder holder = (MaterialsAuthenticationViewHolder) viewHolder;
            PostVipAuthentication.VipDateModeBean bean = vipDateModeBeanList.get(position);
            holder.txt_name.setText("《" + bean.getDataName() + "》");
            holder.txt_platform.setText(bean.getDataRemarks());
            final int type = bean.getType();
            holder.roundImageView.setVisibility(type == 5 ? View.VISIBLE : View.GONE);
            if (type == 5) {
                String dataImg = bean.getDataImg();
                String imgUrl = null;
                if (dataImg.contains("|")) {
                    imgUrl = dataImg.substring(0, dataImg.indexOf("|"));
                } else {
                    imgUrl = dataImg;
                }
                GlideUtils.loadImageView(context, holder.roundImageView, imgUrl,
                        R.drawable.penpal_default_logo, R.drawable.penpal_default_logo);
            }
        }
    }

    @Override
    public int getItemCount() {
        return vipDateModeBeanList == null ? 0 : vipDateModeBeanList.size();
    }

    public class MaterialsAuthenticationViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_name)
        TextView txt_name;
        @BindView(R.id.txt_platform)
        TextView txt_platform;
        @BindView(R.id.roundImageView)
        RoundImageView roundImageView;

        public MaterialsAuthenticationViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * set listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
